#include "StompedReality/Core/FileSystem.h"
#include "StompedReality/Core/IGame.h"
#include "StomperLib/Controls/NewProject.h"
#include <Gwen/Controls/Property/Folder.h>
#include <assert.h>
#include <json/json.h>
namespace StomperLib {
namespace Controls {
GWEN_CONTROL_CONSTRUCTOR( NewProject )
{
  // setup buttons
  this->m_Title->SetText( L"New Project" );
  this->m_OKButton->SetText( L"Create" );
  this->m_OKButton->onDown.Add( this, &NewProject::OnCreate );
  this->onCancel.Add( this, &NewProject::ClearAll );

  // create properties
  Gwen::Controls::PropertyTree* pTree = new Gwen::Controls::PropertyTree( this );
  pTree->SetMargin( Gwen::Margin( 5, 5, 5, 5 ));
  pTree->Dock( Gwen::Pos::Fill );
  Gwen::Controls::Properties* props = pTree->Add( L"Project" );
  m_NameRow = props->Add( L"Name", L"asd" );
  m_NameRow->onChange.Add( this, &NewProject::UpdateFinalPath );
  m_PathRow = props->Add( L"Path", new Gwen::Controls::Property::Folder( props ), L"" );
  m_PathRow->onChange.Add( this, &NewProject::UpdateFinalPath );
  m_FinalPathRow = props->Add( L"Output Path", L"" );
  pTree->ExpandAll( );
  this->ClearAll( );
}
void NewProject::ClearAll( )
{
  this->m_NameRow->GetProperty( )->SetPropertyValue( "" );
  this->m_PathRow->GetProperty( )->SetPropertyValue( "" );
  this->m_FinalPathRow->GetProperty( )->SetPropertyValue( "" );
}

void NewProject::OnCreate( )
{
  assert( this->m_Game != 0 );

  // create Paths
  Gwen::String path = this->m_FinalPathRow->GetProperty( )->GetPropertyValue( ).Get( );
  Gwen::String gfxPath = path;
  gfxPath.append( "/" ).append( "gfx" );
  this->m_Game->getFileSystem( )->createDirectory( path.c_str( ));
  this->m_Game->getFileSystem( )->createDirectory( gfxPath.c_str( ));
  this->Hide( );

  // create project config
  Json::Value root;
  root["Name"] = this->m_NameRow->GetProperty( )->GetPropertyValue( ).Get( ).c_str( );
  root["NextTextureID"] = 1;
  root["NextPatchID"] = 1;
  Gwen::Event::Information createInfo( this );
  Gwen::String fPath = this->m_FinalPathRow->GetProperty( )->GetPropertyValue( ).Get( );
  fPath.append( "\\" ).append( this->m_NameRow->GetProperty( )->GetPropertyValue( ).Get( )).append( ".srprj" );
  this->m_Game->getFileSystem( )->saveJsonAbsolute( root, fPath );

  // send event
  createInfo.String = Gwen::TextObject( fPath );
  this->onCreateProject.Call( this, createInfo );
}

void NewProject::UpdateFinalPath( )
{
  if (this->m_NameRow->GetProperty( )->GetPropertyValue( ).length( ) > 0 &&
      this->m_PathRow->GetProperty( )->GetPropertyValue( ).length( ) > 0)
  {
    Gwen::String concat = "";
    concat.append( this->m_PathRow->GetProperty( )->GetPropertyValue( ).Get( ) );
    concat.append( "\\" );
    concat.append( this->m_NameRow->GetProperty( )->GetPropertyValue( ).Get( ) );
    this->m_FinalPathRow->GetProperty( )->SetPropertyValue( concat );
    this->m_OKButton->SetDisabled( false );
  } else
  {
    this->m_FinalPathRow->GetProperty( )->SetPropertyValue( "" );
    this->m_OKButton->SetDisabled( true );
  }
}

}
}